#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#include <pthread.h>
int ktask_running = 0;
int ktask_kill = 0;


static sigset_t ksigs;
void* routine_signal(void* arg)
{
    
    int dunno;
    int ret = 0;
    while(1) {
        ret = sigwait(&ksigs, &dunno);
        if(0 != ret) {
            fprintf(stderr, "sigwait error.\n");
            sleep(1);
        }
        else {
            ktask_kill = 1;
        }
    }

    return NULL;
}


void* routine_task(void* arg)
{
    ktask_running = 1;

    while(!ktask_kill) {
        printf("doing task.\n");
        sleep(1);
    }

    ktask_running = 0;

    return NULL;
}


int main()
{
    /* 各调用的返回值判断请自行添加. */
    sigemptyset(&ksigs);
    sigaddset(&ksigs, SIGUSR1);
    pthread_sigmask(SIG_BLOCK, &ksigs, NULL);
    
    pthread_t pth;
    pthread_create(&pth, NULL, routine_signal, NULL);
    pthread_create(&pth, NULL, routine_task, NULL);

    while(1) {
        if(ktask_running) {
            printf("thread is running.\n");
        }
        else {
            printf("thread is not running.\n");
        }

        sleep(1);
    }

    return 0;
}
